package com.micro.modual.service.impl;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.ObjectUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.LoginAuth;
import com.micro.modual.entity.SystemUser;
import com.micro.modual.entity.SystemUserRole;
import com.micro.modual.extension.LoginAuthExtension;
import com.micro.modual.extension.SystemUserExtension;
import com.micro.modual.extension.SystemUserRoleExtension;
import com.micro.modual.extension.UserExtension;
import com.micro.modual.mapper.*;
import com.micro.modual.model.bo.systemuser.BindRoleBO;
import com.micro.modual.model.bo.systemuser.ModifyUserStatusBO;
import com.micro.modual.model.bo.systemuser.SystemUserBO;
import com.micro.modual.model.dto.systemuserrole.UserRoleDTO;
import com.micro.modual.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;


/**
*  <h1>系统用户表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:09
 */
@Service("systemUserService")
public class SystemUserServiceImpl extends AbstractBasicsServiceImpl<SystemUser,Long> implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private LoginAuthMapper loginAuthMapper;

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    @Autowired
    private SystemUserPostMapper systemUserPostMapper;

    @Autowired
    private SystemRoleMapper systemRoleMapper;

    /**
     * <h2>新增系统用户表详情</h2>
     *
     * @param systemUserBO {@link SystemUserBO} 系统用户表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSystemUser(SystemUserBO systemUserBO) {
        //1.初始化用户基础信息
        String passWordSalt = GeneratorCodeUtil.generatorSalt();
        String passWord = DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(MicroConstant.SystemDefaultValue.DEAFULT_PASSWORD.getBytes()) + passWordSalt).getBytes());
        String userCode = GeneratorCodeUtil.generatorSystemCode();
        SystemUser systemUser = SystemUserExtension.converAddSystemUser(systemUserBO, userCode, passWordSalt);
        systemUserMapper.insertSelective(systemUser);
        //2.保存用户登录授权信息
        List<LoginAuth> loginAuthList = new ArrayList<>();
        LoginAuth userNameLoginAuth = LoginAuthExtension.createSystemLoginAuth(userCode, passWord, MicroConstant.AuthType.AUTH_USER_NAME, systemUserBO.getLoginName());
        loginAuthList.add(userNameLoginAuth);
        if (ObjectUtil.isNotEmpty(systemUserBO.getUserPhone())) {
            LoginAuth phoneLoginAuth = LoginAuthExtension.createSystemLoginAuth(userCode, passWord, MicroConstant.AuthType.AUTH_PHONE, systemUserBO.getUserPhone());
            loginAuthList.add(phoneLoginAuth);
        }
        if (ObjectUtil.isNotEmpty(systemUserBO.getUserEmail())) {
            LoginAuth emailLoginAuth = LoginAuthExtension.createSystemLoginAuth(userCode, passWord, MicroConstant.AuthType.AUTH_EMAIL, systemUserBO.getUserEmail());
            loginAuthList.add(emailLoginAuth);
        }
        loginAuthMapper.insertListKeySelectiveMapper(loginAuthList);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>根据主键编号删除系统用户表信息</h2>
     *
     * @param id 系统用户表 主键编号
     * @return {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSystemUser(Long id) {
        SystemUser systemUser = systemUserMapper.selectByPrimaryKey(id);
        systemUserRoleMapper.deleteByUserCode(systemUser.getUserCode());
        systemUserPostMapper.deleteByUserCode(systemUser.getUserCode());
        loginAuthMapper.deleteByCustomerCode(systemUser.getUserCode());
        systemUserMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改用户状态{包含批量}</h2>
     *
     * @param modifyUserStatusBO {@link ModifyUserStatusBO} 修改用户状态请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson modifyUserStatus(ModifyUserStatusBO modifyUserStatusBO) {
        SystemUser systemUser = systemUserMapper.selectByPrimaryKey(modifyUserStatusBO.getUserId());
        systemUser.setUserStatus(modifyUserStatusBO.getUserStatus());
        systemUserMapper.updateByPrimaryKeySelective(systemUser);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>校验邮箱</h2>
     *
     * @param email 邮箱
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson validateEmailExist(String email) {
        if (systemUserMapper.findEmailExist(email) > 0) {
            return new RespJson(MicroResponseCode.EMAIL_HAS_EXIST.getResponseCode(), MicroResponseCode.EMAIL_HAS_EXIST.getResponseMessage());
        } else {
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

    /**
     * <h2>校验手机号是否存在</h2>
     *
     * @param phone 手机号
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson validataPhoneExist(String phone) {
        if (systemUserMapper.findPhoneExist(phone) > 0) {
            return new RespJson(MicroResponseCode.PHONE_HAS_EXIST.getResponseCode(), MicroResponseCode.PHONE_HAS_EXIST.getResponseMessage());
        } else {
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

    /**
     * <h2>校验用户名是否存在</h2>
     *
     * @param loginName 用户名/登录名
     * @return {@link RespJson} 返回对象
     */
    @Override
    public RespJson validataLoginName(String loginName) {
        if (loginAuthMapper.findLoginNameilExist(loginName) > 0) {
            return new RespJson(MicroResponseCode.LOGIN_NAME_HAS_EXIST.getResponseCode(), MicroResponseCode.LOGIN_NAME_HAS_EXIST.getResponseMessage());
        } else {
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());

        }
    }

    @Override
    public RespJsonData<UserRoleDTO> findUserRole(Long userId) {
        SystemUser systemUser = systemUserMapper.selectByPrimaryKey(userId);
        List<String> roleList = systemUserRoleMapper.findRoleListByUserCode(systemUser.getUserCode());
        UserRoleDTO userRoleDTO = SystemUserExtension.converUserRoleDTO(systemUser,roleList);
        return new RespJsonData(userRoleDTO, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJson bindRole(BindRoleBO bindRoleBO) {
        systemUserRoleMapper.deleteByUserCode(bindRoleBO.getUserCode());
        List<SystemUserRole> systemUserRoleList = SystemUserExtension.converBindUserRole(bindRoleBO);
        systemUserRoleMapper.insertListKeySelectiveMapper(systemUserRoleList);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }
}
